package com.sgb.kjwl.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;

public class TextViewUtils {

    /**
     * 改变textview 图片
     */
    public static void changeTextView(Context context, TextView textView, int img) {
        Resources res = context.getResources();
        Drawable imgs = res.getDrawable(img);
        imgs.setBounds(0, 0, imgs.getMinimumWidth(), imgs.getMinimumHeight());
        textView.setCompoundDrawables(imgs, null, null, null);
    }
    public static void changeTextViewRightIcon(Context context, TextView textView, int img) {
        Resources res = context.getResources();
        Drawable imgs = res.getDrawable(img);
        imgs.setBounds(0, 0, imgs.getMinimumWidth(), imgs.getMinimumHeight());
        textView.setCompoundDrawables(null, null, imgs, null);
    }
    /**
     * 改变textview 图片和文字选择颜色
     */
    public static void changeTextView(Context context, TextView textView, int img, boolean isSelect) {
        textView.setSelected(isSelect);
        Resources res = context.getResources();
        Drawable imgs = res.getDrawable(img);
        imgs.setBounds(0, 0, imgs.getMinimumWidth(), imgs.getMinimumHeight());
        textView.setCompoundDrawables(null, null, imgs, null);
    }

    public static void changeTextView(Context context, TextView textView, int selectImg,int openImg,int noSelectImg, boolean isSelect,boolean isOpen,@ColorRes int selectColor,@ColorRes int noSelectColor) {
        if (isOpen && isSelect){
            throw new RuntimeException("open and select can not all true");
        }

        if (selectImg == 0 && noSelectImg == 0 && openImg == 0){
            throw new RuntimeException("selectImg、 noSelectImg and openImg can not all 0");
        }

        Resources res = context.getResources();
        if (isSelect){
            textView.setTextColor(res.getColor(selectColor));
        }else {
            textView.setTextColor(res.getColor(noSelectColor));
        }
        Drawable imgs;
        if (isSelect){
            imgs = res.getDrawable(selectImg);
        }else if (isOpen){
            imgs = res.getDrawable(openImg);
        }else {
            imgs = res.getDrawable(noSelectImg);
        }

        imgs.setBounds(0, 0, imgs.getMinimumWidth(), imgs.getMinimumHeight());
        textView.setCompoundDrawables(null, null, imgs, null);
    }
}
